﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using LGE;

namespace MagicFight
{
    public class StartScreen : GameScreen
    {
        int vkCallerIndex = -1;

        List<Character> characters;
        public StartScreen()
            : base()
        {
            
        }

        public override void LoadContent()
        {
            characters = new List<Character>();
        }

        public override void Update(GameTime gameTime)
        {
            #region Virtual Keyboard
            if (vkCallerIndex > -1)
            {
                String name = Input.GetVKResult;
                if (name != "WAITING" && name != null)
                {
                    if (name != "CANCEL" && name != "")
                    {
                        Character c = new Character(vkCallerIndex, name, Engine.Load<Texture2D>("Images\\Characters\\P" + (vkCallerIndex + 1)));
                        GameScreenManager.Push(new MagicSelectionScreen(c));
                        characters.Add(c);
                    }
                    vkCallerIndex = -1;
                }
                return;
            }
            #endregion

            UpdateInput();
        }

        public void UpdateInput()
        {
            for (int i = 0; i < 4; ++i) // i == playerIndex
            {
                if (Input.Pressed(i, KeyMap.A))
                {
                    if (GetCharacter(i) == null)
                    {
                        Input.CallVK("", "");
                        vkCallerIndex = i;
                        return;
                    }
                    else GameScreenManager.Push(new MagicSelectionScreen(GetCharacter(i)));
                }
                else if (Input.Pressed(i, KeyMap.B))
                {
                    characters.Remove(GetCharacter(i));
                }
                else if (Input.Pressed(i, KeyMap.Start))
                {
                    if (characters.Count < 2) return;
                    GameScreenManager.Pop();
                    GameScreenManager.Push(new PlayScreen(characters));
                }
            }
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            //spriteBatch.GraphicsDevice.Clear(Color.DarkGray);
            spriteBatch.DrawString(MagicFight.Font, "Aperte A para participar da luta e escolher as magias!\nAperte START para iniciar a luta!", new Vector2(100, 50), Color.White);

            Vector2 gridPos = new Vector2(100, 100);
            int playerSpacing = 300;
            for (int i = 0; i < 4; ++i) // i == playerIndex        
            {
                spriteBatch.DrawString(MagicFight.Font, "Player " + (i + 1), gridPos + new Vector2(i * playerSpacing, 0), Color.White);
                if (GetCharacter(i) == null) spriteBatch.DrawString(MagicFight.Font, "DESCONECTADO", gridPos + new Vector2(i * playerSpacing, 100), Color.White);
                else
                {
                    spriteBatch.DrawString(MagicFight.Font, GetCharacter(i).name, gridPos + new Vector2(i * playerSpacing, 40), Color.White);
                    GetCharacter(i).position = gridPos + new Vector2(i * playerSpacing + 25, 100);
                    GetCharacter(i).Draw(spriteBatch);
                }
            }
        }

        public Character GetCharacter(int i)
        {
            foreach (Character c in characters)
                if (c.playerIndex == i) return c;
            return null;
        }
    }
}   
